﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Axe.Data.Mapping
{
    public static class SqlClauseBuilder
    {
        internal static string ToSql(MappingDescription descr,Condition c)
        {
            string fieldName = descr.MappedFields[c.PropertyName].MappedName;
            string opText = GetOperator(c.Operator);

            return String.Format("{0}{1}@{0}", fieldName,opText);
        }

        internal static void RegisterParameter(SqlCommand cmd, MappingDescription descr, Condition c)
        {
            string fieldName = descr.MappedFields[c.PropertyName].MappedName;
            cmd.Parameters.AddWithValue("@" + fieldName, c.Value);
        }

        public static string GetOperator(ConditionOperator op)
        {
            switch (op)
            {
                case ConditionOperator.Equals:
                    return "=";
                case ConditionOperator.NotEquals:
                    return "!=";
                case ConditionOperator.Like:
                    return "LIKE";
                case ConditionOperator.Greater:
                    return ">";
                case ConditionOperator.GreaterOrEq:
                    return ">=";
                case ConditionOperator.Less:
                    return "<";
                case ConditionOperator.LessOrEq:
                    return "<=";
                default:
                    throw new InvalidOperationException();
            }
        }
    }
}
